import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:image_picker/image_picker.dart';
import 'package:merchants/models/merchant.dart';

class MerchantCreateScreen extends StatefulWidget {
  const MerchantCreateScreen({super.key});

  @override
  State<MerchantCreateScreen> createState() => _MerchantCreateScreenState();
}

class _MerchantCreateScreenState extends State<MerchantCreateScreen> {
  final _formKey = GlobalKey<FormBuilderState>();

  Future<void> _submitForm() async {
    if (_formKey.currentState?.saveAndValidate() ?? false) {
      final formData = _formKey.currentState!.value;

      final merchant = Merchant(
        name: formData['name'],
        // 店名
        miniProgramName: formData['mini_program_name'],
        // 小程序名称
        // 小程序图标
        legalPersonName: formData['legal_person_name'],
        legalPersonPhone: formData['legal_person_phone'],
      );

      debugPrint('提交商户数据: ${merchant.toJson()}');

      if (mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(const SnackBar(content: Text('商户创建成功')));
        Navigator.of(context).pushReplacementNamed('/merchant/home');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('创建商户'),
        actions: [
          IconButton(icon: const Icon(Icons.check), onPressed: _submitForm),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: FormBuilder(
          key: _formKey,
          child: Column(
            children: [
              const SizedBox(height: 20),
              FormBuilderTextField(
                name: 'name',
                decoration: const InputDecoration(
                  labelText: '商户名/店名',
                  prefixIcon: Icon(Icons.store),
                  border: OutlineInputBorder(),
                ),
                validator: FormBuilderValidators.compose([
                  FormBuilderValidators.required(),
                  FormBuilderValidators.maxLength(50),
                ]),
              ),
              const SizedBox(height: 16),
              FormBuilderTextField(
                name: 'mini_program_name',
                decoration: const InputDecoration(
                  labelText: '小程序名称',
                  prefixIcon: Icon(Icons.apps),
                  border: OutlineInputBorder(),
                ),
                validator: FormBuilderValidators.compose([
                  FormBuilderValidators.required(),
                  FormBuilderValidators.maxLength(50),
                ]),
              ),
              const SizedBox(height: 16),
              // 法人姓名
              FormBuilderTextField(
                name: 'legal_person_name',
                decoration: const InputDecoration(
                  labelText: '法人姓名',
                  prefixIcon: Icon(Icons.person),
                  border: OutlineInputBorder(),
                ),
                validator: FormBuilderValidators.compose([
                  FormBuilderValidators.required(),
                  FormBuilderValidators.maxLength(50),
                ]),
              ),
              const SizedBox(height: 16),
              // 法人电话
              FormBuilderTextField(
                name: 'legal_person_phone',
                decoration: const InputDecoration(
                  labelText: '法人电话',
                  prefixIcon: Icon(Icons.phone),
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.phone,
                validator: FormBuilderValidators.compose([
                  FormBuilderValidators.required(),
                  FormBuilderValidators.match(
                    RegExp(r'^[0-9]{11}$'),
                    errorText: '请输入11位手机号',
                  ),
                ]),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
